/*
* Copyright 2017 Yonyou Auto Information Technology（Shanghai） Co., Ltd. All Rights Reserved.
*
* This software is published under the terms of the YONYOU Software
* License version 1.0, a copy of which has been included with this
* distribution in the LICENSE.txt file.
*
* @Project Name : marketing-service-wechatpush
*
* @File name : MessageHistoryController.java
*
* @Author : Administrator
*
* @Date : 2017年8月16日
*
----------------------------------------------------------------------------------
*     Date       Who       Version     Comments
* 1. 2017年8月16日    Administrator    1.0
*
*
*
*
----------------------------------------------------------------------------------
*/

package com.yonyou.gmmc.service.wechatpush.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.marketing.common.Response;
import org.marketing.common.constants.CommonConstant;
import org.marketing.common.constants.ResultMsgConstants;
import org.marketing.common.event.BizEvent;
import org.marketing.common.util.CheckUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.yonyou.gmmc.service.wechatpush.common.ApiVersionConsts;
import com.yonyou.gmmc.service.wechatpush.model.TtMessage;
import com.yonyou.gmmc.service.wechatpush.service.MessageHistoryService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @author Administrator
 * @date 2017年8月16日
 */
@RestController
@RequestMapping(value = ApiVersionConsts.BASE_PATH + ApiVersionConsts.VERSION + "/myMessage")
@Api("我的消息相关API")
public class MessageHistoryController {

    private static final Logger   logger = Logger.getLogger(MessageHistoryController.class);

    @Autowired
    private MessageHistoryService messageHistoryService;

    @RequestMapping(value = "/messageList", method = RequestMethod.GET, produces = "application/json;UTF-8")
    @ApiOperation(value = "查看我的消息列表", notes = "查看我的消息列表")
    public Response<List<TtMessage>> MessageList(HttpServletRequest request
    		,@RequestParam(required = false, defaultValue = "") String userId_) {
        String userId = "";
        List<TtMessage> result = new ArrayList<TtMessage>();
        try {
        	if(!CheckUtil.NullOrEmpty(userId_)){
        		userId = userId_;
        	}else{
        		userId = request.getHeader(CommonConstant.HEAD_USER_ID);
        	}
            result = messageHistoryService.MessageList(userId);
            return new Response<>(ResultMsgConstants.RESULT_CODE_SUCCESS, result, "");
        } catch (Exception e) {
            logger.error("查询状态发生异常", e);
            return new Response<>(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
        }
    }

    @RequestMapping(value = "/message", method = RequestMethod.GET, produces = "application/json;UTF-8")
    @ApiOperation(value = "查看我的消息详情", notes = "查看我的消息详情")
    public Response<List<TtMessage>> Message(@RequestParam("id") String id) {
        logger.info("该消息的ID为：" + id);
        List<TtMessage> result = new ArrayList<>();
        try {
            result = messageHistoryService.Message(id);
            return new Response<>(ResultMsgConstants.RESULT_CODE_SUCCESS, result, "");
        } catch (Exception e) {
            logger.error("查询状态发生异常", e);
            return new Response<>(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
        }
    }

    @RequestMapping(value = "/newMessage", method = RequestMethod.POST)
    @ApiOperation(value = "新增消息记录", notes = "新增消息记录")
    public Response<Map<String, Object>> SendMessage(@RequestBody BizEvent event) {
        Map<String, Object> result = new HashMap<>();
        try {
            result = messageHistoryService.SendMessage(event);
            return new Response<>(ResultMsgConstants.RESULT_CODE_SUCCESS, result, "");
        } catch (Exception e) {
            logger.error("新增消息记录发生异常", e);
            return new Response<>(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
        }
    }

    @RequestMapping(value = "/readingFlag", method = RequestMethod.GET, produces = "application/json;UTF-8")
    @ApiOperation(value = "更新消息状态为已读", notes = "更新消息状态为已读")
    public Response<Map<String, Object>> updateMessageType(@RequestParam("id") String id) {
        Map<String, Object> result = new HashMap<>();
        try {
            result = messageHistoryService.updateMessageType(id);
            return new Response<>(ResultMsgConstants.RESULT_CODE_SUCCESS, result, "");
        } catch (Exception e) {
            logger.error("更新消息状态发生异常", e);
            return new Response<>(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
        }
    }

    @RequestMapping(value = "/messageCount", method = RequestMethod.GET, produces = "application/json;UTF-8")
    @ApiOperation(value = "查看未读消息数", notes = "查看未读消息数")
    public Response<Integer> MessageCount(HttpServletRequest request
    		,@RequestParam(required = false, defaultValue = "") String userId_) {
    	String userId = "";
    	Integer result = 0;
        try {
        	if(!CheckUtil.NullOrEmpty(userId_)){
        		userId = userId_;
        	}else{
        		userId = request.getHeader(CommonConstant.HEAD_USER_ID);
        	}
            result = messageHistoryService.MessageCount(userId);
            return new Response<>(ResultMsgConstants.RESULT_CODE_SUCCESS, result, "");
        } catch (Exception e) {
            logger.error("查询状态发生异常", e);
            return new Response<>(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
        }
    }
}
